package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.BleBeaconDao;
import com.tbit.main.h2.pojo.ParkPoint;
import com.tbit.main.pojo.BleBeacon;
import com.tbit.main.pojo.ParkPointBoundCount;
import com.tbit.main.service.BleBeaconService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2020-11-16 18:05
 * 道钉
 */
@Service("bleBeaconService")
public class BleBeaconServiceImpl implements BleBeaconService {

    @Autowired
    private BleBeaconDao bleBeaconDao;

    @Override
    public void insert(BleBeacon bleBeacon) {
        bleBeacon.setAddTime(DateTimeUtil.getNowTime());
        bleBeacon.setState(0);
        bleBeaconDao.insert(bleBeacon);
    }

    @Override
    public List<BleBeacon> queryPage(Integer state, Integer parkPointId, Integer pageNO, Integer rowCount) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        return bleBeaconDao.queryPage(params);
    }

    @Override
    public List<ParkPoint> getBleBeacon(Integer accountId) {
        return bleBeaconDao.getBleBeacon(accountId);
    }

    @Override
    public List<ParkPoint> getBleBeaconRegionId(List<Integer> regionId) {
        return bleBeaconDao.getBleBeaconRegionId(StringUtil.getListString(regionId));
    }

    @Override
    public Integer queryCount(Integer state, Integer parkPointId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parkPointId", parkPointId);
        params.put("state", state);
        return bleBeaconDao.queryCount(params);
    }

    @Override
    public List<ParkPointBoundCount> getByStateId(Integer state, List<Integer> parkPointId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parkPointId", StringUtil.getListString(parkPointId));
        params.put("state", state);
        return bleBeaconDao.getByStateId(params);
    }

    @Override
    public List<String> addBatch(Integer parkPointId, List<String> machineNOs, Integer accountId) {
        /**检查是否存在*/
        List<String> exitMachineNOs = bleBeaconDao.getByMachineNOs(machineNOs);
        if (exitMachineNOs.size() > 0) {
            return exitMachineNOs;
        }
        List<BleBeacon> list = new LinkedList<>();
        for (String machineNO : machineNOs) {
            BleBeacon bleBeacon = new BleBeacon();
            bleBeacon.setState(1);
            bleBeacon.setAccountId(accountId);
            bleBeacon.setAddTime(DateTimeUtil.getNowTime());
            bleBeacon.setMachineNO(machineNO);
            bleBeacon.setParkPointId(parkPointId);
            list.add(bleBeacon);
        }
        bleBeaconDao.addBatch(list);
        return null;
    }

    @Override
    public void delete(Integer bleBeaconId) {
        bleBeaconDao.del(bleBeaconId);
    }

    @Override
    public void delNO(String machineNO) {
        bleBeaconDao.delNO(machineNO);
    }

    @Override
    public void updateTime(List<String> machineNOs) {
        bleBeaconDao.updateTime(StringUtil.getStringListString(machineNOs));
    }

    @Override
    public void update(String machineNO, Integer state, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNO", machineNO);
        map.put("state", state);
        map.put("remark", remark);
        bleBeaconDao.update(map);
    }

    @Override
    public List<Map<String, Object>> getDevice(List<Integer> parkPoints, Integer time, Integer state) {
        Map<String, Object> map = new HashMap<>();
        map.put("parkPoints", StringUtil.getListString(parkPoints));
        map.put("time", DateTimeUtil.getNowDateTimeBefore(-time));
        map.put("state", state);
        return bleBeaconDao.getDevice(map);
    }

    @Override
    public List<BleBeacon> getByNOTime(List<String> machineNOs, String time) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNOs", StringUtil.getStringListString(machineNOs));
        map.put("time", time);
        return bleBeaconDao.getByNOTime(map);
    }

    @Override
    public void updateState(List<String> machineNOs, Integer state) {
        bleBeaconDao.updateState(StringUtil.getStringListString(machineNOs), state);
    }

    @Override
    public List<BleBeacon> getTime(String time, List<Integer> parkPointId, Integer state, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("time", time);
        map.put("parkPointIds", StringUtil.getListString(parkPointId));
        map.put("state", state);
        map.put(DaoParamConstant.accountId, accountId);
        return bleBeaconDao.getTime(map);
    }

    @Override
    public List<ParkPointBoundCount> getTimeCount(String time, List<Integer> parkPointIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("time", time);
        map.put("parkPointIds", StringUtil.getListString(parkPointIds));
        return bleBeaconDao.getTimeCount(map);
    }

    @Override
    public List<ParkPointBoundCount> getAllId(List<Integer> parkPointIds) {
        return bleBeaconDao.getAllId(StringUtil.getListString(parkPointIds));
    }

    @Override
    public BleBeacon getByMachineNO(String machineNO) {
        return bleBeaconDao.getByMachineNO(machineNO);
    }
}
